package com.micromall.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.micromall.common.base.BaseController;
import com.micromall.common.utils.Charsets;
import com.micromall.common.utils.Dict;
import com.micromall.common.utils.PageInfo;
import com.micromall.model.MiMember;
import com.micromall.service.IMiMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * <p>
 * 会员表  前端控制器
 * </p>
 * @author Dante Fung
 * @since 2017-07-26
 */
@Controller
@RequestMapping("/miMember")
public class MiMemberController extends BaseController {
    
    @Autowired private IMiMemberService miMemberService;
    
    @GetMapping("/manager")
    public String manager() {
        return "admin/miMember/miMemberList";
    }


    @PostMapping("/dataGrid")
    @ResponseBody
    public PageInfo dataGrid(MiMember miMember, Integer page, Integer rows, String sort,String order) {
        EntityWrapper<MiMember> ew = new EntityWrapper<MiMember>();
        ew.setEntity(miMember);
        Page<MiMember> pages = getPage(page, rows, sort, order);
        pages = miMemberService.selectPage(pages,ew);
        return pageToPageInfo(pages);
    }
    
    /**
     * 添加页面
     * @return
     */
    @GetMapping("/addPage")
    public String addPage() {
        return "admin/miMember/miMemberAdd";
    }

    /**
     * 添加
     * @param 
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Object add(MiMember miMember) {
        miMember.setCreateTime(new Date());
        miMember.setDataStatus(Dict.YesOrNo.Yes.getCode());
        boolean b = miMemberService.insert(miMember);
        if (b) {
            return renderSuccess("添加成功！");
        } else {
            return renderError("添加失败！");
        }
    }

    /**
     * 删除
     * @param memberNo
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public Object delete(String memberNo) {
        MiMember miMember = new MiMember();
        miMember.setMemberNo(memberNo);
    miMember.setDataStatus(Dict.YesOrNo.No.getCode());
        boolean b = miMemberService.updateById(miMember);
        if (b) {
            return renderSuccess("删除成功！");
        } else {
            return renderError("删除失败！");
        }
    }

    /**
     * 编辑
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/editPage")
    public String editPage(Model model, Long id) {
        MiMember miMember = miMemberService.selectById(id);
        model.addAttribute("miMember", miMember);
        return "admin/miMember/miMemberEdit";
    }

    /**
     * 编辑
     * @param 
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(MiMember miMember) {
        boolean b = miMemberService.updateById(miMember);
        if (b) {
            return renderSuccess("编辑成功！");
        } else {
            return renderError("编辑失败！");
        }
    }


    @RequestMapping("/login")
    @ResponseBody
    public Object  login(HttpServletRequest request, HttpServletResponse response){
        response.setCharacterEncoding(Charsets.UTF_8.toString());
        response.setHeader("Content-Type","text/html;charset=utf-8");
        //String smsCode = request.getAttribute("smsCode");
        String smsCode = request.getParameter("smsCode");
        String phoneNo = request.getParameter("phoneNo");
        HttpSession session = request.getSession();
        String session_smsCode = (String )session.getAttribute("smsCode");
        //判断该手机号是否注册
//        EntityWrapper<SysUser> ew = new EntityWrapper<>();
//        SysUser user = new SysUser();
//        user.setUserCode("WS"+phoneNo);
//        ew.setEntity(user);
//        SysUser sysUser = sysUserService.selectOne(ew);
        EntityWrapper<MiMember> ew = new EntityWrapper<>();
        MiMember miMember = new MiMember();
        //1000 自动注册成功  2000 自动注册失败  3000 用户已经存在
        String  statusCode =  miMemberService.hadExistAutoRegister(phoneNo,"");
        if("1000" == statusCode){
            //注册该账号，并且登录
            miMember.setMemberNo("WS"+phoneNo);
            ew.setEntity(miMember);
            MiMember cuurMiMember =  miMemberService.selectById(ew);
            //验证码错误。从新登录
            if(!smsCode.equals(session_smsCode)){
                return write2Json("lOGIN_FAIL", Boolean.FALSE,"登陆失败!请重新登陆", null);
            }
            session.setAttribute("cuurMiMember",cuurMiMember);
            return write2Json("REGISTER_SUCCESS", Boolean.TRUE,"注册成功！且登陆成功", null);
        }
        if("3000" == statusCode) {
            miMember.setMemberNo("WS"+phoneNo);
            ew.setEntity(miMember);
            MiMember cuurMiMember =  miMemberService.selectOne(ew);
            //验证码错误。从新登录
            if(!smsCode.equals(session_smsCode)){
                return write2Json("lOGIN_FAIL", Boolean.FALSE,"登陆失败!请重新登陆", null);
            }
            session.setAttribute("cuurMiMember",cuurMiMember);
//            return write2Json(null, Boolean.TRUE,"登陆成功!", null);
            return write2Json("lOGIN_SUCCESS", Boolean.TRUE,"登陆成功!", null);
        }
        //自动注册失败
//        if("3000" == statusCode){
            return write2Json("REGISTER_FAIL", Boolean.FALSE,"自动注册失败", null);
//        }
    }

    /**
     * 跳转到登陆页
     * @return
     */
    @RequestMapping("/loginPage")
    public String loginPage(HttpServletRequest request){
        return "/cart/login";
    }

    /**
     * 跳转到购物车列表登陆页
     * @return
     */
    @RequestMapping("/cartList_loginPage")
    public String cartList_loginPage(HttpServletRequest request){
        return "/cart/cartList_login";
    }

}
